package com.yxh.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yxh.base.PageEntity;
import com.yxh.dao.GroupMapper;
import com.yxh.dao.GroupMenuMapper;
import com.yxh.dao.UserMapper;
import com.yxh.exception.MenuException;
import com.yxh.exception.UnitException;
import com.yxh.pojo.Group;
import com.yxh.pojo.GroupMenu;
import com.yxh.pojo.Unit;
import com.yxh.pojo.User;
import com.yxh.pojo.vo.GroupMenuVO;
import com.yxh.service.GroupService;
import com.yxh.util.SnowflakeIdWorker;
import com.yxh.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;
import java.util.List;

import static com.yxh.util.APIErrorCode.PARAMS_INVALID;
import static com.yxh.util.APIErrorCode.ROLE_NOT_DELETE_ERROR;

/**
 * Created by asus on 2018/5/31.
 */

@Service
@Transactional
public class GroupServcieImpl implements GroupService {

    @Autowired
    private GroupMapper groupDao;

    @Autowired
    private GroupMenuMapper groupMenuMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public PageInfo getInformation(Group deptJson, Integer limit, Integer offset) {
        PageHelper.startPage(limit, offset);
        List<Group> groupList = groupDao.selectBygGroup(deptJson);
        PageInfo pageInfo = new PageInfo(groupList);
        return pageInfo;
    }

    @Override
    public PageInfo getInformationGroupMenu(Group group, Integer limit, Integer offset) {
        PageHelper.startPage(limit, offset);
        List<GroupMenu> groupMenuList = groupMenuMapper.selectByGroupId(group.getId());
        PageInfo pageInfo = new PageInfo(groupMenuList);
        return pageInfo;
    }

    @Override
    public void saveGroupMenu(GroupMenuVO groupMenuVO) {
        GroupMenu groupMenu = new GroupMenu();
        groupMenuMapper.deleteByGroupId(groupMenuVO.getGroupId());
        for (String menuId: groupMenuVO.getMenuIds()) {
            groupMenu.setMenuId(menuId);
            groupMenu.setGroupId(groupMenuVO.getGroupId());
            groupMenu.setId(SnowflakeIdWorker.creatId());
            groupMenuMapper.insert(groupMenu);
        }

    }

    @Override
    public List<GroupMenu> selectByGroupId(String groupId) {
        return groupMenuMapper.selectByGroupId(groupId);
    }

    @Override
    public int deleteGroup(String id) {
        if(!StringUtil.isEmpty(id)){
            String [] arr = id.split(",");
            int count = groupMenuMapper.deleteByIds(arr);
            if (count == 0) {
                throw new UnitException(PARAMS_INVALID);
            }
        }
        return 0;
    }

    @Override
    public int save(Group record) {
        record.setId(SnowflakeIdWorker.creatId());
        try {
            return groupDao.insert(record);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }


    }

    @Override
    public int update(Group record) {
        try {
            return groupDao.update(record);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }

    }

    @Override
    public int delete(String id) {
        if(!StringUtil.isEmpty(id)){
            String [] arr = id.split(",");
            User u=new User();
            u.setRole(id);
           if(userMapper.selectByUser(u).size()!=0)
               throw new MenuException(ROLE_NOT_DELETE_ERROR);
            int count = groupDao.deleteByIds(arr);
            if (count == 0) {
                throw new UnitException(PARAMS_INVALID);
            }
        }
        return 0;
    }



    @Override
    public void batchDelete(List<Group> records) {

    }

    @Override
    public Group get(String id) {
        return null;
    }



    @Override
    public Group get(Group record) {
        return groupDao.getEntity(record);
    }

    @Override
    public List<Group> getList(Group record) {
        return null;
    }

    @Override
    public List<Group> getAllList(Group record) {
        return null;
    }

    @Override
    public List<Group> getListPage(PageEntity<Group> record) {
        return null;
    }


}
